// Appendix2.3
log using Appendix2.3.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_pilot_2024_dta_20240319.dta" // American National Election Study 2024 Pilot Study, March 19, 2024 Version. Date accessed: March 09, 2025.

keep if sample_type == 1 // Only keep those respondents with proper weight, as advised in codebook

//Demographic items
*Delete missing values and rename
replace faminc_new = . if inlist(faminc_new, 97, -7) 
replace ideology_lcself= . if inlist(ideology_lcself, -1, -7) 
rename gender FemaleGender

*Generate dummies
gen Graduate=. 
replace Graduate=0 if inrange(educ, 1, 4) 
replace Graduate=1 if inlist(educ, 5, 6)

gen BIPOC=. 
replace BIPOC=0 if race==1
replace BIPOC=1 if inrange(race, 2, 7)

//Create social justice scale
*Delete missing values
replace group_antifa= . if inlist(group_antifa, -7, 999)

*Generate a dummy variable for raceadvantage
gen raceadwhite=. 
replace raceadwhite=1 if raceadvt_white==3 & raceadvt_whitestr==1
replace raceadwhite=2 if raceadvt_white==3 & raceadvt_whitestr==2
replace raceadwhite=3 if raceadvt_white==3 & raceadvt_whitestr==3
replace raceadwhite=4 if raceadvt_white==2
replace raceadwhite=5 if raceadvt_white==1 & raceadvt_whitestr==3
replace raceadwhite=6 if raceadvt_white==1 & raceadvt_whitestr==2
replace raceadwhite=7 if raceadvt_white==1 & raceadvt_whitestr==1

*Reverse code certain items so social justice values are high
foreach var in police_number trans_health school_gender {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa {
    summarize `var'
    gen s_`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* At this point, the scale has a Cronbach's Alpha of 0.82

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in s_rschool_gender s_rtrans_health s_rpolice_number s_raceadwhite s_group_blm s_group_antifa {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_score = 0
gen count_nonzero = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in s_rschool_gender s_rtrans_health s_rpolice_number s_raceadwhite s_group_blm s_group_antifa {
    replace total_score = total_score + `var'
    replace count_nonzero = count_nonzero + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = . 
replace SocJusValues = total_score / count_nonzero if count_nonzero > 0

// Standardize
egen Age = std(age)
egen Income = std(faminc_new)

// Regression models
regress SocJusValues Age BIPOC FemaleGender Graduate Income [pweight=weight], robust 
eststo 
regress SocJusValues Age BIPOC FemaleGender Graduate Income if ideology_lcself<4 [pweight=weight], robust 
eststo 
esttab

log close

eststo clear
esttab using "Appendix2.3.rtf", ///
    b(3) se(3) ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    coeflabels(_cons "Constant") ///
    nonumbers replace

// Alternative specification
ologit SocJusValues Age FemaleGender Graduate Income BIPOC [pweight=weight], robust 

